package com.huadao.pda.project.projectDetail;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.huadao.pda.R;
import com.huadao.pda.base.BaseActivity;
import com.huadao.pda.base.BasePresenter;
import com.huadao.pda.base.BaseScanDialogFragment;
import com.huadao.pda.common.GsonUtil;
import com.huadao.pda.data.bean.DeviceInfo;
import com.huadao.pda.data.bean.Project;
import com.huadao.pda.main.scanView.ScanDF;
import com.huadao.pda.material.MaterialListDF;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProjectDetailDF extends BaseScanDialogFragment implements ProjectDetailContract.IProjectDetailView {

    @BindView(R.id.tv_projectName)
    TextView tvProjectName;
    @BindView(R.id.tv_UniqueIdentificationCode)
    TextView tvUniqueIdentificationCode;
    @BindView(R.id.tv_projectId)
    TextView tvProjectId;
    @BindView(R.id.tv_hospitalName)
    TextView tvHospitalName;
    @BindView(R.id.tv_bodyWeight)
    TextView tvBodyWeight;
    @BindView(R.id.tv_deviceName)
    TextView tvDeviceName;
    @BindView(R.id.tv_deviceNumber)
    TextView tvDeviceNumber;
    @BindView(R.id.tv_MacAddress)
    TextView tvMacAddress;
    @BindView(R.id.tv_ip)
    TextView tvIp;
    @BindView(R.id.tv_port)
    TextView tvPort;

    private Project project;
    private ProjectDetailPresenter presenter;
    private boolean isScanLogin;

    public static ProjectDetailDF newInstance(Project project) {

        Bundle args = new Bundle();
        args.putSerializable("project",project);

        ProjectDetailDF fragment = new ProjectDetailDF();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick(R.id.btn_scanMaterial)
    public void onViewClicked() {
        MaterialListDF materialListDF=MaterialListDF.newInstance(project);
        materialListDF.show(getFragmentManager(),null);
    }

    @OnClick(R.id.btn_scanLogin)
    public void onClickScanLogin(){
        isScanLogin=true;
        startScan();
        ScanDF scanDF=new ScanDF();
        scanDF.show(getFragmentManager(),null);
    }

    @Override
    protected BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_detail;
    }

    @Override
    protected void onInit() {
        presenter = new ProjectDetailPresenter(this);
        setBackVisibility(View.VISIBLE);
        setTitle("查看");
        Bundle bundle=getArguments();
        if(bundle!=null){
            project= (Project) bundle.get("project");
        }

        if (project.getProjectName() != null) {
            initProjectData();
            presenter.getDeviceByDeviceId(project.getDeviceId1());
        }
    }

    @Override
    public void onGetDevice(DeviceInfo deviceInfo) {
        if(deviceInfo!=null) {
            initDeviceData(deviceInfo);
        }
    }

    @Override
    public void onScanLogin() {
        isScanLogin=false;
//        showToast("登陆成功");
    }

    private void initDeviceData(DeviceInfo deviceInfo) {
        tvDeviceName.setText(deviceInfo.getDeviceName());
        tvDeviceNumber.setText(deviceInfo.getDeviceId());
        tvMacAddress.setText(deviceInfo.getMacAddress());
        tvIp.setText(deviceInfo.getIpAddress());
        tvPort.setText(deviceInfo.getProt());
    }

    private void initProjectData() {
        tvProjectName.setText(project.getProjectName());
        tvUniqueIdentificationCode.setText(project.getIdentifier());
        tvProjectId.setText(project.getId());
        tvHospitalName.setText(project.getHospitalName());
        tvBodyWeight.setText(String.valueOf(project.getWeight()));
    }

    @Override
    public void onScanResult(String barcode) {
        Log.i(TAG, barcode);
        if(isScanLogin) {
            String loginCode=null;
            try {
                JSONObject jsonObject=new JSONObject(barcode);
                if(jsonObject.has("loginCode")){
                    loginCode=jsonObject.getString("loginCode");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            DeviceInfo deviceInfo = GsonUtil.getInstance().fromJson(barcode, DeviceInfo.class);
            if(project!=null&&deviceInfo!=null&&loginCode!=null) {
                presenter.scanLogin(project.getId(), deviceInfo.getId(), loginCode);
            }else {
                showToast("登陆信息错误");
            }
        }
    }
}
